<template>
  <div class="card-list" ref="content">
    <a-table
      :columns="columns"
      :dataSource="loadData"
      :pagination="false"
      :loading="memberLoading"
    >
      <template slot="operation" slot-scope="text, record">
        <span >
          <a @click="$refs.editModal.go(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm title="是否要删除此行？" @confirm="remove(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </template>
    </a-table>
    <a-button style="width: 100%; margin-top: 16px; margin-bottom: 8px" type="dashed" icon="plus" @click="$refs.createModal.go(token(),pmsArId(),nowMg())">添加</a-button>
    <discount-add ref="createModal"  @ok="getBasedate" />
    <discount-edit ref="editModal"  @ok="getBasedate" />
  </div>
</template>

<script>
import { mixin, mixinDevice } from '@/utils/mixin'
import { getDiscountList, delDiscount } from '@/api/manage'
import { mapGetters } from 'vuex'
import DiscountAdd from './modules/DiscountAdd'
import DiscountEdit from './modules/DiscountEdit'
export default {
  name: 'FeeDiscount',
  mixins: [mixin, mixinDevice],
  components: {
    DiscountAdd,
    DiscountEdit
  },
  data () {
    return {
      description: '新增优惠券，优惠券可对具体项目优惠',
      extraImage: 'https://gw.alipayobjects.com/zos/rmsportal/RzwpdLnhmvDJToTdfDPe.png',
      loading: false,
      memberLoading: false,
      // table
      columns: [
        {
          title: '名称',
          dataIndex: 'name',
          key: 'name',
          width: '20%',
          scopedSlots: { customRender: 'name' }
        },
        {
          title: '分类',
          dataIndex: 'type_text',
          key: 'type_text',
          width: '20%',
          scopedSlots: { customRender: 'type_text' }
        },
        {
          title: '优惠',
          dataIndex: 'content',
          key: 'content',
          width: '20%',
          scopedSlots: { customRender: 'content' }
        },
        {
          title: '创建时间',
          dataIndex: 'create_time',
          key: 'create_time',
          width: '20%',
          scopedSlots: { customRender: 'create_time' }
        },
        {
          title: '操作',
          key: 'action',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      loadData: [],
      errors: [],
      skipItem: '',
      goItem: ''
    }
  },
  created () {
    this.getBasedate()
  },
  methods: {
    ...mapGetters(['token', 'pmsArId', 'nowMg']),
    getBasedate () {
      this.memberLoading = true
      getDiscountList({ token: this.token(), ar_id: this.pmsArId(), cm_mg_id: this.nowMg() }).then(res => {
        this.memberLoading = false
        if (res.code === 0) {
          this.loadData = res.data
        } else {
          this.$notification['error']({
            message: '错误',
            description: res.msg,
            duration: 4
          })
        }
      })
    },
    remove (id) {
      delDiscount({ token: this.token(), ar_id: this.pmsArId(), cm_mg_id: this.nowMg(), id: id }).then(res => {
        if (res.code == 0) {
          this.getBasedate()
        } else {
          this.$notification['error']({
            message: '错误',
            description: res.msg,
            duration: 4
          })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
  .card-list{
    background: #fff;
  }
</style>
